package StudyCode.summary.mycollection;

import java.lang.reflect.Array;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class AddsTheObjectToTheArray {
	@SuppressWarnings("unchecked")
	public static <T> T[] add(T[] array, T obj) {
		if (array == null && obj == null) {
			return null;
		}
		Class<?> clazz = obj != null ? obj.getClass() : array.getClass()
				.getComponentType();
		int length = array != null ? array.length : 0;
		T[] newArray = (T[]) Array.newInstance(clazz, length + 1);
		if (array != null) {
			System.arraycopy(array, 0, newArray, 0, length);
		}
		newArray[length] = obj;
		return newArray;
	}
}
